﻿using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.Model.Enums;
using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.WebApi.Entities;
using Microsoft.EntityFrameworkCore;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 课程支付
    /// </summary>
    public partial class PayService
    {
        /// <summary>
        /// 课程订单支付逻辑
        /// </summary>
        private async Task<UnaryResult<PayResultDto>> PayCourseOrderAsync(PayRequestDto request, PayOrderInnerInfo payInner, PayResultDto payResult, BaseUser user)
        {
            var courseOrder = await _courseOrderRepository.FirstOrDefaultAsync(m => m.ID == request.OrderId && m.State == CourseOrderStateEnum.NotPay);
            if (courseOrder == null)
            {
                return UnaryResult.From(10, payResult, $"未查询到业务订单：{request.OrderId}");
            }

            courseOrder.PayType = request.PayType;
            await _courseOrderRepository.UpdateAsync(courseOrder);

            payInner.OrderNo = courseOrder.OrderNo;
            payInner.ProductName = courseOrder.Name;
            payInner.ProductDescription = courseOrder.Remark;
            payInner.OrderAmount = courseOrder.OrderAmount;
            payInner.DiscountAmount = courseOrder.BonusAmount + courseOrder.CouponAmount;
            payInner.PayAmount = courseOrder.PayAmount;
            payInner.PayState = PayOrderStateEnum.NotPay;
            payInner.PayRemark = $"{DateTimeOffset.Now},{courseOrder.Name} 报名";
            payInner.IsCompatibleVenueOrder = false;
            payInner.Payer = user.U_OpenId;

            return UnaryResult.Succeed(payResult);
        }

        /// <summary>
        /// 课程订单退款逻辑
        /// </summary>
        private async Task<UnaryResult<RefundResultDto>> ExecuteCourseOrderRefundAsync(RefundRequestDto request, PayOrder payOrder, RefundResultDto result)
        {
            var courseOrder = await _courseOrderRepository.FirstOrDefaultAsync(m => m.ID == request.OrderId);
            if (courseOrder == null)
            {
                result.Msg = "未查询到业务订单";
                return UnaryResult.From(10, result, result.Msg);
            }

            if (courseOrder.State == CourseOrderStateEnum.Cancel)
            {
                result.Msg = "订单已取消";
                return UnaryResult.From(10, result, result.Msg);
            }

            if (courseOrder.State == CourseOrderStateEnum.Completed || courseOrder.State == CourseOrderStateEnum.Refunded)
            {
                result.Msg = "订单已完成，无法执行退款";
                return UnaryResult.From(10, result, result.Msg);
            }

            //进行相关判断及赋值
            //实际待取消参赛人员ID
            if (courseOrder.State == CourseOrderStateEnum.NotPay)
            {
                courseOrder.State = CourseOrderStateEnum.Cancel;
                await _courseOrderRepository.UpdateAsync(courseOrder);
                result.IsSuccess = true;
                result.Msg = "未支付取消订单";
                return UnaryResult.Succeed(result);
            }

            if (request.CancelType != OrderCancelTypeEnum.User)
            {
                result.ApplyAmount = request.ApplyAmount;
                result.RefundAmount = request.ApplyAmount;
            }
            else
            {
                if (courseOrder.CourseType == CourseTypeEnum.Times)
                {
                    if (courseOrder.IsNotRefund)
                    {
                        result.Msg = "课程次数已用完";
                        return UnaryResult.From(10, result, result.Msg);
                    }

                    if (courseOrder.CreateTime.Value.AddHours(24) < DateTimeOffset.Now)
                    {
                        result.Msg = "订单超过24小时";
                        return UnaryResult.From(10, result, result.Msg);
                    }
                }
                else
                {
                    if (courseOrder.StartTime < DateTimeOffset.Now)
                    {
                        result.Msg = "课程已开始";
                        return UnaryResult.From(10, result, result.Msg);
                    }
                }

                result.ApplyAmount = courseOrder.PayAmount;
                result.RefundAmount = courseOrder.PayAmount;
            }

            if (result.ApplyAmount <= 0)
            {
                result.Msg = "退款金额小于等于0";
                return UnaryResult.From(10, result, result.Msg);
            }

            if (payOrder.PayAmount < payOrder.RefundAmount + result.ApplyAmount)
            {
                result.Msg = "订单未退金额不足";
                return UnaryResult.From(10, result, result.Msg);
            }

            return UnaryResult.Succeed(result);
        }
    }
}
